package com.eduService.teacher.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduService.teacher.entity.EduCourse;
import com.eduService.teacher.entity.EduCourseDescription;
import com.eduService.teacher.entity.EduTeacher;
import com.eduService.teacher.mapper.EduCourseMapper;
import com.eduService.teacher.service.IEduCourseDescriptionService;
import com.eduService.teacher.service.IEduCourseService;
import com.eduService.teacher.vo.course.courseConditionVo;
import com.eduService.teacher.vo.course.courseMes;
import com.eduService.teacher.vo.course.homeCourseConditionVo;
import com.eduService.teacher.vo.course.homeCourseInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements IEduCourseService {
    @Autowired
    IEduCourseDescriptionService iEduCourseDescriptionService;

    @Override
    public String saveCourse(courseMes courseMes) {
        System.out.println(courseMes);
        EduCourse eduCourse = new EduCourse();
    //        调用MybatisPlus底层生成雪花ID
        IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator();
        courseMes.setId(identifierGenerator.nextId(new Object()).toString());

        BeanUtils.copyProperties(courseMes,eduCourse);
        System.out.println(eduCourse);
        boolean isSave = this.save(eduCourse);
        if (!isSave ){
            System.out.println("添加课程失败！");
        }


        String courseId = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseId);
        BeanUtils.copyProperties(courseMes,eduCourseDescription);
        iEduCourseDescriptionService.save(eduCourseDescription);

        return courseId;
    }
/*
* 根据id获取课程信息
* */
    @Override
    public courseMes getCourseInfo(String courseid) {
        courseMes courseMes = new courseMes();
        EduCourse eduCourse = baseMapper.selectById(courseid);
        System.out.println(eduCourse);
        BeanUtils.copyProperties(eduCourse,courseMes);
        System.out.println(courseMes);
        EduCourseDescription description = iEduCourseDescriptionService.getById(courseid);
        System.out.println(description);
        courseMes.setDescription(description.getDescription());
        return courseMes;
    }

    /*
    *
    * 更新课程信息
    * */
    @Override
    public void updateCourseInfo(courseMes courseMes) {
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseMes.getDescription());
        description.setId(courseMes.getId());
         iEduCourseDescriptionService.updateById(description);

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseMes,eduCourse);
        baseMapper.updateById(eduCourse);
    }
/*
*
* 条件查询课程内容
* */
    @Override
    public void pageQueryCourse(Page<EduCourse> tpage, courseConditionVo conditionVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("view_count");
        if (conditionVo == null){
            baseMapper.selectPage(tpage,queryWrapper);
            return ;
        }
//            获取对象各值
        String title = conditionVo.getTitle();
        BigDecimal priceLow = conditionVo.getPriceLow();
        BigDecimal priceHigh = conditionVo.getPriceHigh();
        String teacherId = conditionVo.getTeacherId();
        System.out.println(conditionVo.isStatus());

        if (!conditionVo.isStatus()){
           String status = "Draft";
            queryWrapper.eq("status",status);
        }else{
            String status = "Normal ";
            queryWrapper.eq("status",status);
        }

//          判断各值是否为空，不为空就添加到模糊查询条件中
        if (!StringUtils.isEmpty(title))queryWrapper.like("title",title);
        //eq相当于“=”
        if (priceLow != null)queryWrapper.ge("price",priceLow);
        //ge相当于“>=”
        if (priceHigh != null)queryWrapper.le("price",priceHigh);
        //le相当于“<=”
        if (!StringUtils.isEmpty(teacherId))queryWrapper.eq("teacher_id",teacherId);

//          根据嵌入条件进行分页查询
        baseMapper.selectPage(tpage,queryWrapper);
    }

    /**
     * （前台）根据条件进行课程查询
     * */
    @Override
    public void pageQueryCourseHome(Page<EduCourse> tpage, homeCourseConditionVo homeCourseConditionVo) {
       if(homeCourseConditionVo == null){
           baseMapper.selectPage(tpage,null);
           return;
       }

        QueryWrapper<EduCourse> queryWrapper  = new QueryWrapper<>();
        if (!StringUtils.isEmpty(homeCourseConditionVo.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id",homeCourseConditionVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(homeCourseConditionVo.getSubjectId())){
            queryWrapper.eq("subject_id",homeCourseConditionVo.getSubjectId());
        }

        if(!StringUtils.isEmpty(homeCourseConditionVo.getBuyCountSort())){
            queryWrapper.orderByAsc("view_count");
        }

        if(!StringUtils.isEmpty(homeCourseConditionVo.getPriceSort())){
            queryWrapper.orderByAsc("price");
        }

        if(!StringUtils.isEmpty(homeCourseConditionVo.getGmtCreateSort())){
            queryWrapper.orderByAsc("gmt_create");
        }

        baseMapper.selectPage(tpage,queryWrapper);

        return ;

    }

    @Override
    public homeCourseInfo getCourseInfoMes(String courseid) {
        return baseMapper.getCourseInfoMes(courseid);
    }

    @Override
    public void paySuccessCount(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        eduCourse.setBuyCount(eduCourse.getBuyCount()+1);
        baseMapper.updateById(eduCourse);

    }


}
